#!/usr/bin/python

"""
    VODie
    kitesurfing@kitesurfing.ie
"""

import re
import sys
from BeautifulSoup import BeautifulStoneSoup
import urllib, urllib2
import MenuConstants
import simplejson

# Channel Constants
CHANNEL = 'Shoutcast Radio by Country'
LOGOICON = 'http://www.rte.ie/radio/images/logo.gif'

GOOGLE_URL = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&'

COUNTRY_CODE={
"Afghanistan":"countryAF",
"Albania":"countryAL",
"Algeria":"countryDZ",
"American Samoa":"countryAS",
"Andorra":"countryAD",
"Angola":"countryAO",
"Anguilla":"countryAI",
"Antarctica":"countryAQ",
"Antigua and Barbuda":"countryAG",
"Argentina":"countryAR",
"Armenia":"countryAM",
"Aruba":"countryAW",
"Australia":"countryAU",
"Austria":"countryAT",
"Azerbaijan":"countryAZ",
"Bahamas":"countryBS",
"Bahrain":"countryBH",
"Bangladesh":"countryBD",
"Barbados":"countryBB",
"Belarus":"countryBY",
"Belgium":"countryBE",
"Belize":"countryBZ",
"Benin":"countryBJ",
"Bermuda":"countryBM",
"Bhutan":"countryBT",
"Bolivia":"countryBO",
"Bosnia and Herzegovina":"countryBA",
"Botswana":"countryBW",
"Bouvet Island":"countryBV",
"Brazil":"countryBR",
"British Indian Ocean Territory":"countryIO",
"Brunei Darussalam":"countryBN",
"Bulgaria":"countryBG",
"Burkina Faso":"countryBF",
"Burundi":"countryBI",
"Cambodia":"countryKH",
"Cameroon":"countryCM",
"Canada":"countryCA",
"Cape Verde":"countryCV",
"Cayman Islands":"countryKY",
"Central African Republic":"countryCF",
"Chad":"countryTD",
"Chile":"countryCL",
"China":"countryCN",
"Christmas Island":"countryCX",
"Cocos (Keeling) Islands":"countryCC",
"Colombia":"countryCO",
"Comoros":"countryKM",
"Congo":"countryCG",
"Congo, country Democratic Republic":"countryCD",
"Cook Islands":"countryCK",
"Costa Rica":"countryCR",
"Cote d'Ivoire":"countryCI",
"Croatia":"countryHR",
"Cyprus":"countryCY",
"Czech Republic":"countryCZ",
"Denmark":"countryDK",
"Djibouti":"countryDJ",
"Dominica":"countryDM",
"Dominican Republic":"countryDO",
"East Timor":"countryTL",
"Ecuador":"countryEC",
"Egypt":"countryEG",
"El Salvador":"countrySV",
"Equatorial Guinea":"countryGQ",
"Eritrea":"countryER",
"Estonia":"countryEE",
"Ethiopia":"countryET",
"Falkland Islands (Malvinas)":"countryFK",
"Faroe Islands":"countryFO",
"Fiji":"countryFJ",
"Finland":"countryFI",
"France":"countryFR",
"France, country Metropolitan":"countryFX",
"French Guiana":"countryGF",
"French Polynesia":"countryPF",
"French Southern Territories":"countryTF",
"Gabon":"countryGA",
"Gambia":"countryGM",
"Georgia":"countryGE",
"Germany":"countryDE",
"Ghana":"countryGH",
"Gibraltar":"countryGI",
"Greece":"countryGR",
"Greenland":"countryGL",
"Grenada":"countryGD",
"Guadeloupe":"countryGP",
"Guam":"countryGU",
"Guatemala":"countryGT",
"Guinea":"countryGN",
"Guinea-Bissau":"countryGW",
"Guyana":"countryGY",
"Haiti":"countryHT",
"Heard and McDonald Islands":"countryHM",
"Honduras":"countryHN",
"Hong Kong":"countryHK",
"Hungary":"countryHU",
"Iceland":"countryIS",
"India":"countryIN",
"Indonesia":"countryID",
"Iraq":"countryIQ",
"Ireland":"countryIE",
"Israel":"countryIL",
"Italy":"countryIT",
"Jamaica":"countryJM",
"Japan":"countryJP",
"Jordan":"countryJO",
"Kazakhstan":"countryKZ",
"Kenya":"countryKE",
"Kiribati":"countryKI",
"Kuwait":"countryKW",
"Kyrgyzstan":"countryKG",
"Lao People's Democratic Republic":"countryLA",
"Latvia":"countryLV",
"Lebanon":"countryLB",
"Lesotho":"countryLS",
"Liberia":"countryLR",
"Libya":"countryLY",
"Liechtenstein":"countryLI",
"Lithuania":"countryLT",
"Luxembourg":"countryLU",
"Macau":"countryMO",
"Macedonia":"countryMK",
"Madagascar":"countryMG",
"Malawi":"countryMW",
"Malaysia":"countryMY",
"Maldives":"countryMV",
"Mali":"countryML",
"Malta":"countryMT",
"Marshall Islands":"countryMH",
"Martinique":"countryMQ",
"Mauritania":"countryMR",
"Mauritius":"countryMU",
"Mayotte":"countryYT",
"Mexico":"countryMX",
"Micronesia":"countryFM",
"Moldova":"countryMD",
"Monaco":"countryMC",
"Mongolia":"countryMN",
"Montserrat":"countryMS",
"Morocco":"countryMA",
"Mozambique":"countryMZ",
"Namibia":"countryNA",
"Nauru":"countryNR",
"Nepal":"countryNP",
"Netherlands":"countryNL",
"Netherlands Antilles":"countryAN",
"New Caledonia":"countryNC",
"New Zealand":"countryNZ",
"Nicaragua":"countryNI",
"Niger":"countryNE",
"Nigeria":"countryNG",
"Niue":"countryNU",
"Norfolk Island":"countryNF",
"Northern Mariana Islands":"countryMP",
"Norway":"countryNO",
"Oman":"countryOM",
"Pakistan":"countryPK",
"Palau":"countryPW",
"Palestinian Territory":"countryPS",
"Panama":"countryPA",
"Papua New Guinea":"countryPG",
"Paraguay":"countryPY",
"Peru":"countryPE",
"Philippines":"countryPH",
"Pitcairn":"countryPN",
"Poland":"countryPL",
"Portugal":"countryPT",
"Puerto Rico":"countryPR",
"Qatar":"countryQA",
"Reunion":"countryRE",
"Romania":"countryRO",
"Russian Federation":"countryRU",
"Rwanda":"countryRW",
"Saint Kitts and Nevis":"countryKN",
"Saint Lucia":"countryLC",
"Saint Vincent and the Grenadines":"countryVC",
"Samoa":"countryWS",
"San Marino":"countrySM",
"Sao Tome and Principe":"countryST",
"Saudi Arabia":"countrySA",
"Senegal":"countrySN",
"Serbia and Montenegro":"countryCS",
"Seychelles":"countrySC",
"Sierra Leone":"countrySL",
"Singapore":"countrySG",
"Slovakia":"countrySK",
"Slovenia":"countrySI",
"Solomon Islands":"countrySB",
"Somalia":"countrySO",
"South Africa":"countryZA",
"South Georgia and The South Sandwich Islands":"countryGS",
"South Korea":"countryKR",
"Spain":"countryES",
"Sri Lanka":"countryLK",
"St. Helena":"countrySH",
"St. Pierre and Miquelon":"countryPM",
"Suriname":"countrySR",
"Svalbard and Jan Mayen Islands":"countrySJ",
"Swaziland":"countrySZ",
"Sweden":"countrySE",
"Switzerland":"countryCH",
"Taiwan":"countryTW",
"Tajikistan":"countryTJ",
"Tanzania":"countryTZ",
"Thailand":"countryTH",
"Togo":"countryTG",
"Tokelau":"countryTK",
"Tonga":"countryTO",
"Trinidad and Tobago":"countryTT",
"Tunisia":"countryTN",
"Turkey":"countryTR",
"Turkmenistan":"countryTM",
"Turks and Caicos Islands":"countryTC",
"Tuvalu":"countryTV",
"Uganda":"countryUG",
"Ukraine":"countryUA",
"United Arab Emirates":"countryAE",
"United Kingdom":"countryGB",
"United States":"countryUS",
"United States Minor Outlying Islands":"countryUM",
"Uruguay":"countryUY",
"Uzbekistan":"countryUZ",
"Vanuatu":"countryVU",
"Vatican":"countryVA",
"Venezuela":"countryVE",
"Viet Nam":"countryVN",
"Virgin Islands (British)":"countryVG",
"Virgin Islands (U.S.)":"countryVI",
"Wallis and Futuna Islands":"countryWF",
"Western Sahara":"countryEH",
"Yemen":"countryYE",
"Zambia":"countryZM",
"Zimbabwe":"countryZW"
}

class ShoutcastRadio:

    def getChannelDetail(self):
        return None
        return {'Channel'  : CHANNEL,
                'Thumb'    : LOGOICON,
                'Title'    : CHANNEL,
                'mode'     : MenuConstants.MODE_MAINMENU,
                'Plot'     : CHANNEL
                }

    def getMainMenu(self):
        for country in COUNTRY_CODE.keys():
            yield {'Channel' : CHANNEL,
                   'Thumb'   : LOGOICON,
                   'url'     : COUNTRY_CODE[country],
                   'Title'   : country,
                   'mode'    : MenuConstants.MODE_CREATEMENU}
    
    def getMenuItems(self, countryCode):
        query = urllib.urlencode({'q'   : '((intitle:"SHOUTcast Administrator")|(intitle:"shoutcast Administrator"))&((intext:"U SHOUTcast D.N.A.S. Status")|(intext:"U shoutcast dnas Status")&(intext:"ie"))',
                                  'rsz' : '8',
                                  'hl'  : 'en',
                                 })
        
        print GOOGLE_URL + query
        
        page = urllib2.urlopen(GOOGLE_URL + query)
        json = simplejson.loads(page.read())
        page.close()
        results = json['responseData']['results']
        for i in results:
            yield {'Channel' : CHANNEL,
                   'Thumb'   : CHANNEL,
                   'url'     : i['url'] + 'listen.pls',
                   'Title'   : i['content'],
                   'mode'    : MenuConstants.MODE_PLAYRADIO}        
            
if __name__ == '__main__':
    # Test Main Menu
    for menuitem in ShoutcastRadio().getMainMenu():
        print menuitem

    # Test Types
    for radio in ShoutcastRadio().getMenuItems("countryIE"):
        print radio
